package com.woniu.userservice.service.impl;

import com.woniu.userservice.dao.UsersDao;
import com.woniu.userservice.entity.Account;
import com.woniu.userservice.dao.AccountDao;
import com.woniu.userservice.entity.ResultBody;
import com.woniu.userservice.entity.Users;
import com.woniu.userservice.service.AccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountDao accountDao;

    @Resource
    private UsersDao usersDao;

    @Override
    public Account queryById(Integer id) {
        return accountDao.queryById(id);
    }

    @Override
    public List<Account> queryAllByLimit(int offset, int limit) {
        return accountDao.queryAllByLimit(offset, limit);
    }
   
     @Override
    public List<Account> findAll() {
        return accountDao.findAll();
    }

    @Override
    public Account insert(Account account) {
        accountDao.insert(account);
        return account;
    }

    @Override
    public Account update(Account account) {
        accountDao.update(account);
        return queryById(account.getId());
    }

    @Override
    public boolean deleteById(Integer id) {
        return accountDao.deleteById(id) > 0;
    }
/**
*@Author:huangjing
*@Description:
*@Date: 10:34 2020/3/24
*/
    @Override
    public List queryCountByTime() {
       List registerCount= accountDao.queryCountByTime();
        return registerCount;
    }

    @Override
    public ResultBody login(String account, String pass) {
        Account account1=accountDao.queryByAccount(account);
        if (account1==null){
            return ResultBody.error("账号不存在！请先注册!");
        }
        if (account1.getPassword().equals(pass)){
            //返回用户id
            return ResultBody.success(account1.getUserid());
        }
        return ResultBody.error("账号或密码错误！");
    }

    @Override
    public ResultBody register(String userName,String account, String pass) {
        //验证账号是否注册过
        Account account1=accountDao.queryByAccount(account);
        if (account1!=null){
            return ResultBody.error("账号已存在！");
        }
        //插入用户
        Users users = new Users();
        users.setPhone(account);
        users.setName(userName);
        usersDao.insert(users);
        //插入账号
        account1=new Account();
        account1.setAccount(account);
        account1.setPassword(pass);
        account1.setStatus(0);
        account1.setUserid(users.getId());
        accountDao.insert(account1);
        return ResultBody.success(users.getId());
    }
}